import gdata.docs.data
import gdata.docs.client
import gdata.data
import os
import os.path
        
def upload_file(google_user, google_password, file_name, mime_type):
    client = get_client(google_user, google_password)
    file_pointer = open(file_name)
    media_source = gdata.data.MediaSource(file_handle=file_pointer, content_type=mime_type, content_length=os.path.getsize(file_pointer.name))
    entry = client.Upload(media_source, file_pointer.name)

def get_user_and_password():
    google_user =raw_input("Google account: ")
    google_password =raw_input("Google password: ")
    return google_user, google_password

def get_client(google_user, google_password):
    client = gdata.docs.client.DocsClient(source="docloader")
    client.ssl = True
    client.http_client.debug = False
    client.ClientLogin(google_user, google_password, client.source)
    return client

def get_file_name():
    return raw_input("Enter filename: ")

def get_mime_type(file_name):
    return gdata.docs.data.MIMETYPES[file_name[file_name.rindex(".")+1:].upper()]

#If proxy with password
#os.environ['http_proxy'] = 'http://19.12.2.44:83' 
#os.environ['https_proxy'] = 'https://19.12.2.44:83' 
#os.environ['proxy-username'] = '' 
#os.environ['proxy-password'] = '' 

google_user, google_password = get_user_and_password()
file_name = get_file_name()
mime_type = get_mime_type(file_name)
print "Uploading " + file_name
upload_file(google_user, google_password, file_name, mime_type)
print "Uploading done"
